﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Windows.Resources;
using System.Xml.Linq;
using System.Xml;
using System.Collections.Generic;

namespace Cmic.Grammar.WP
{
    public class Category
    {
        public string Id
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public virtual string Uri
        {
            get 
            {
                return string.Format("/SubCategoryPage.xaml?categoryId={0}",this.Id);
            }
        }

        private static List<Category> categoryList;
        public static List<Category> GetCategoryList()
        {
            if (categoryList == null)
            {
                categoryList = new List<Category>();

                XElement xmlDoc = XElement.Load("Content/Index.xml");
                var categoryNodeList = from category in xmlDoc.Descendants("category")
                                       select category;
                foreach (var categoryNode in categoryNodeList)
                {
                    Category category = new Category
                    {
                        Id = categoryNode.Attribute("id").Value,
                        Name = categoryNode.Attribute("name").Value
                    };
                    categoryList.Add(category);
                }
            }
            return categoryList;
        }

        public static Category GetCategoryById(string categoryId)
        {
            Category category = GetCategoryList().Single<Category>(S => S.Id == categoryId);
            return category;
        }

        private static List<SubCategory> subcategoryList;
        public static List<SubCategory> GetSubCategoryListById(string categoryId)
        {
            if (subcategoryList == null)
            {
                subcategoryList = new List<SubCategory>();
                XElement xmlDoc = XElement.Load("Content/Index.xml");
                var categoryNodeList = from category in xmlDoc.Descendants("category")
                                       select category;
                foreach (var categoryNode in categoryNodeList)
                {
                    var subCategoryNodeList = from subCategory in categoryNode.Descendants("subcategory")
                                              select subCategory;
                    foreach (var subCategoryNode in subCategoryNodeList)
                    {
                        SubCategory subCategory = new SubCategory
                        {
                            Id = subCategoryNode.Attribute("id").Value,
                            Name = subCategoryNode.Attribute("name").Value,
                            ParentId = categoryNode.Attribute("id").Value
                        };
                        subcategoryList.Add(subCategory);
                    }
                }
            }

            var list = from subcategory in subcategoryList
                       where subcategory.ParentId == categoryId
                       select subcategory;
            return list.ToList();

        }


    }
}
